const Article = require('../model/article')
/**
 * 获取文章详情
 */
// let redisUtils=require('../utils/registUtil')
let getDetails = async (req, resp) => {
    let query = req.query;
    let id = query.article_id;
    let getDetail = await Article.getDetails(id)
    // let UserData=  await redisUtils.get("UserloginData")
    resp.send(getDetail)
}


/**
 * 搜索获取并且分页z中间件
 */
let searchCent = async (req, resp) => {
    let query = req.query;
    let text = query.text;
    let index = query.index;
    let yenum = query.yenum;
    let searchCents = await Article.searchCent(text, Number(index), Number(yenum));
    resp.send(searchCents)
}
/**
 * 获取所有并且分页z中间件
 */
let getAll = async (req, resp) => {
    let query = req.query;
    let index = query.index;
    let yenum = query.yenum;
    let getAll = await Article.getAll(Number(index), Number(yenum));
    resp.send(getAll)
}

/**
 * 获取热门文章中间件
 * @param req
 * @param resp
 */
let getHot = async (req, resp, next) => {
    let num = req.query.num;
    //获取到的参数要转成数字
    let articleHots = await Article.getHots(Number(num));
    resp.send(articleHots)
}


let Release = async (req, resp) => {
    let body = req.body
    let text = body.text;
    let htmltext = body.htmltext;
    let u_id = body.u_id;
    console.log("uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuw")
    console.log(u_id)
    let imgSrcS = body.imgSrcS;
    let title = body.title;
    let Releases = await Article.Releasemod(text, htmltext, u_id, imgSrcS, title);
    resp.send(Releases)

}

let myHistory = async (req, resp) => {
    let myHistorys = await Article.myHistory(req.u_id, req.query.num, req.query.yenum)
    resp.send(myHistorys)
}
/**
 * 评论
 * @param req
 * @param resp
 * @returns {Promise<void>}
 */
let setComment = async (req, resp) => {
    let body = req.body
    let u_id = body.u_id
    let content = body.content
    let article_id = body.article_id
    Article.setComment(u_id, article_id, content)

    resp.send("ok")
}

let getComment = async (req, resp) => {
    let body = req.body
    let article_id = body.article_id
    let art_Comment = await Article.getComment(article_id)
    console.log(art_Comment)
    resp.send(art_Comment)
}
/**
 * 添加点击率
 * @param req
 * @param resp
 * @returns {Promise<void>}
 */
let addHist = async (req, resp) => {
    let body = req.body
    let article_id = body.article_id
    Article.addHist(article_id)
    resp.send()
}

module.exports = {getHot, Release, searchCent, getDetails, myHistory, setComment, getComment, addHist, getAll}